package com.ma.listener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.ma.business.CalculateMA;
import com.ma.business.StatisticProfits;
import com.ma.business.TradeBusiness;
import com.ma.panel.TradePanel;
import com.ma.vo.TradeParameter;

/**
 * 监听类，触发鼠标动作；
 * 功能包括：计算均线、模拟交易、统计
 * @author zhaosj
 *
 */
public class TradePanelListener extends MouseAdapter {

	private TradePanel panel = null;

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == 1) {

			int bs = s2i(panel.getFieldBuyShort().getSelectedItem().toString());
			int bl = s2i(panel.getFieldBuyLong().getSelectedItem().toString());
			int ss = s2i(panel.getFieldSellShort().getSelectedItem().toString());
			int sl = s2i(panel.getFieldSellLong().getSelectedItem().toString());
			long quantity = s2l(panel.getFieldQuantity().getText());
			int br = s2i(panel.getFieldBuyRate().getSelectedItem().toString());
			int sr = s2i(panel.getFieldSellRate().getSelectedItem().toString());
			String dateStart = panel.getDatepickStart().getText();
			String dateEnd = panel.getDatepickEnd().getText();
			double commision = Double.parseDouble(panel.getFieldCommision().getSelectedItem().toString());
			double stampDuty = Double.parseDouble(panel.getFieldStampDuty().getSelectedItem().toString());

			TradeParameter tp = new TradeParameter();
			tp.setBs(bs);
			tp.setBl(bl);
			tp.setSs(ss);
			tp.setSl(sl);
			tp.setQuantity(quantity);
			tp.setBr(br / 100.0);
			tp.setSr(sr / 100.0);
			tp.setDateEnd(dateEnd);
			tp.setDateStart(dateStart);
			tp.setCommision(commision);
			tp.setStampDuty(stampDuty);

			// 计算均线
			CalculateMA cm = new CalculateMA();
			cm.operateStockMa(tp);

			// 股票交易
			TradeBusiness tb = new TradeBusiness();
			tb.getTradeInfo(tp);

			// 统计单只股票资金量和利润（率）变化情况
			StatisticProfits sp = new StatisticProfits();
			sp.statisticProfits(tp);
			
			System.exit(0);
		}
	}

	/**
	 * 数据型字符串转为int数字
	 * @param str
	 * @return
	 */
	public int s2i(String str) {
		int anIntVar = 0;
		try {
			if (str != null && !"".equals(str))
				anIntVar = Integer.parseInt(str);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

		return anIntVar;
	}

	/**
	 * 数据型字符串转为long数字
	 * @param str
	 * @return
	 */
	public long s2l(String str) {
		long anLongVar = 0l;

		try {
			if (str != null && !"".equals(str))
				anLongVar = Long.parseLong(str);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

		return anLongVar;
	}

	public void setPanel(TradePanel panel) {
		this.panel = panel;
	}
}
